//
//  UILabel+SuggestSize.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/7.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "UILabel+SuggestSize.h"

@implementation UILabel (SuggestSize)

- (CGSize)suggestedSizeForWidth:(CGFloat)width {
    
    return [self suggestedSizeForWidth:width height:MAXFLOAT];
}

- (CGSize)suggestSizeForString:(NSString *)string width:(CGFloat)width {
    
    return [self suggestSizeForString:string width:width height:MAXFLOAT];
}

- (CGSize)suggestedSizeForWidth:(CGFloat)width height:(CGFloat)height {
    
    if (self.attributedText) {
        return [self suggestSizeForAttributedString:self.attributedText width:width height:height];
    }
    return [self suggestSizeForString:self.text width:width height:height];
}

- (CGSize)suggestSizeForString:(NSString *)string width:(CGFloat)width height:(CGFloat)height {
    if (!string) {
        return CGSizeZero;
    }
    return [self suggestSizeForAttributedString:[[NSAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName : self.font}] width:width height:height];
}

- (CGSize)suggestSizeForAttributedString:(NSAttributedString *)string width:(CGFloat)width height:(CGFloat)height {
    
    if (!string) {
        return CGSizeZero;
    }
    
    if (height == 0) {
        height = MAXFLOAT;
    }
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_0
    
    NSDictionary * dic = @{NSFontAttributeName : self.font};
    
    return [string.string boundingRectWithSize:CGSizeMake(width, height)
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                    attributes:dic
                                       context:nil].size;
#else
    return [string.string sizeWithFont:self.font
                     constrainedToSize:CGSizeMake(width, height)
                         lineBreakMode:self.lineBreakMode];
#endif
    
    //#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_0
    //    return [string boundingRectWithSize:CGSizeMake(width, height)
    //                                options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
    //                                context:nil].size;
    //#else
    //    return [string.string sizeWithFont:self.font
    //                     constrainedToSize:CGSizeMake(width,height)
    //                         lineBreakMode:self.lineBreakMode];
    //#endif
    
}




@end
